<div class="container">

    <div class="page-header">
        <div class="row">
            <h1 class="span8"><?
    if($post_action == 'EDIT')
    {
        echo 'Edit';
    }else
        echo 'Add';
?> <?= ucfirst($post_type) ?></h1>
            <span class="span4">

            </span>
        </div>


    </div>

    <div class="row">

        <div class="span8">

            <form class="form-horizontal-qtrans" id="post" method="post" name="post">

                <input type="hidden" value="<?= $post_type; ?>" name="post_type" id="post_type">
                <input type="hidden" value="<?= $post_action; ?>" name="post_action" id="post_action">

                <?
                    if($post_action == 'EDIT')
                    {
                        ?>
                        <input type="hidden" value="<?= $post['ID']; ?>" name="post_ID" id="post_ID">
                    <? }
                ?>

                <fieldset>


                    <div id="post-body">

                        <div id="post-body-content">

                            <!--TITLE-->
                            <? foreach(config_item('wp_languages') as $key => $value): ?>

                                    <div class="postarea">

                                        <h3 class="qtrans_title">Title (<?= $value ?>)</h3>

                                        <div class="qtrans_title_wrap">

                                            <input type="text" name="title[]" class="qtrans_title_input" value="<? if($post_action == 'EDIT') echo $this->translate->find_field($key, $post['post_title']); ?>" />

                                        </div>

                                    </div>

                                <? endforeach; ?>

                            <!-- CONTENT -->
                            <div class="postarea" id="postdivrich">

                                <? foreach(config_item('wp_languages') as $key => $value): ?>

                                        <div class="group">

                                            <span class="group-title"> <?= $value ?> </span>

                                            <div class="editor-holder">
                                                <textarea class="textarea_content" cols="40" rows="10" name="content[]">
                                                    <?
                                                    if($post_action == 'EDIT')
                                                        echo $this->translate->find_field($key, $post['content']);
                                                    ?>
                                                </textarea>
                                            </div>          

                                        </div>

                                    <? endforeach; ?>

                            </div>

                        </div>

                    </div>

                    <div class="parent well" style="margin-top: 23px; margin-bottom: 23px;">
                        <h3 style="padding-left: 0;"><?= ucfirst($post_type) ?> properties</h3>
                        <label for="parent_id" class="screen-reader-text">Parent</label>
                        <select id="parent_id" name="parent_id">
                            <option value="0" selected="selected">(no parent)</option>
                            <?
                                $selected='';
                                foreach($all_posts as $one_post):
                                    ?>
                                    <?
                                    if($one_post['ID'] != $post['ID'])
                                    {
                                        ?>
                                        <?
                                        if($post_action == 'EDIT')
                                        {
                                            if($one_post['ID'] == $post['parent_ID'])
                                            {
                                                $selected='selected="selected"';
                                            }
                                        }
                                        ?>
                                        <option value="<?= $one_post['ID'] ?>" class="level-0" <? echo $selected; ?>><?= $one_post['name'] ?></option>
                                    <? } ?>
                                    <?
                                    $selected='';
                                endforeach;
                            ?>
                        </select>
                    </div>

                    <div class="form-actions">

                        <div class="row">

                            <? if($post_action == 'EDIT'): ?>       

                                    <div class="span1-5" id="delete-action">
                                        <a class="submitdelete deletion delete_post" href="<?= base_url($link.'/delete/'.$post['ID'].'?post_type='.$post_type) ?>">Move to Trash</a>
                                    </div>

                                <? endif; ?> 

                            <span class="<?
                            if($post_action == 'EDIT')
                            {
                                echo 'span2 ml-15';
                            }else
                                echo 'span4';
                            ?> save"><button class="btn btn-primary" type="submit"><i class="icon-white icon-download-alt"></i> Save changes</button></span>
                            <span class="span4 loader"><img id="ajax-loading" class="ajax-loading" src="<?= base_url() ?>assets/img/backgnds/loading.gif" style="visibility: hidden;"></span>
                        </div>

                    </div>

                </fieldset>
            </form>

        </div>

        <div class="span4">

            <div class="hero-unit">
                <h3><?= ucfirst($post_type) ?></h3>
                <p>
                    <a class="btn btn-large" href="<?= base_url($link.'/view_all?post_type='.$post_type) ?>">
                        <i class="icon-list-alt"></i> View All <?= ucfirst($post_type) ?>
                    </a>
                </p>
            </div>

        </div>
    </div>

</div> <!-- /container -->

<!--POST DELETE HANDLER--> 
<script type="text/javascript">
    jQuery(document).ready(function() {

        jQuery('.delete_post').live('click',function(){

            var conf = confirm("Delete <?= ucfirst($post_type) ?>?")
            if (!conf){
                return false;
            }

            window.location.href = jQuery(this).attr('href');

            return false;

        });

    } );
</script>
<!--/POST DELETE HANDLER-->

<!--FORM SUBMIT HANDLER-->
<script>

    jQuery(document).ready(function() {       

        // Override default error message
        jQuery.validator.messages.required = "";

        // Override generation of error label
        $("#post").validate({
            submitHandler: function(form) {

                $('input[type=submit]', '#post').attr('disabled', 'disabled');
                $('#ajax-loading').css('visibility','visible');
                
                tinymce.triggerSave();

                var form = $("#post");
                var form_data = form.serialize();

                $.ajax({
                    url: base_url+'<?= $link ?>'+'/submit_post',
                    dataType: 'json',
                    type: "POST",
                    data: form_data,
                    success: function (data, textStatus, xhr) {
                        jQuery('#ajax-loading').css('visibility','hidden');

                        if(data.action==true){

                            jQuery('.loader').html(data.msg);

                            // REDIRECT
                            function promo_show(){
                                window.location.href = base_url+'<?= $link ?>'+'/view_all?post_type=<?= $post_type ?>'; 
                            }
                            window.setTimeout(function() { promo_show(); }, 1001);

                        }else{

                            $('#post').removeAttr('disabled');                             
                            alert(data.msg)  
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert('Error ocured...\n Server not found.');
                        $('input[type=submit]', '#post').removeAttr('disabled');
                        $('#ajax-loading').css('visibility','hidden');
                    }
                });

            },
            errorPlacement: function(error, element){}
        });

        $('input[type=submit]', '#post').removeAttr('disabled'); 


    });

</script>
<!--/FORM SUBMIT HANDLER--> 